<?php
/**
*
* @package ppkBB3cker
* @version $Id: acp_board_add1_addfields.php 1.000 2009-08-13 12:02:00 PPK $
* @copyright (c) 2009 PPK
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

$user->add_lang('mods/acp/ppkbb3cker_tracker');

if(request_var('add_addfields', 0))
{
	$d_addfields=array();
	foreach(@$_POST['af_name'] as $k=>$v)
	{
		if(@$_POST['af_delete'][$k])
		{
			$d_addfields[]=my_int_val($k);
		}
		else
		{
			$_POST['af_descr'][$k]=$db->sql_escape(utf8_normalize_nfc($_POST['af_descr'][$k]));
			$_POST['af_subject'][$k]=$db->sql_escape(utf8_normalize_nfc($_POST['af_subject'][$k]));
			if($k==0 && @$_POST['af_name'][$k]!='')
			{
				$sql='INSERT INTO '.TRACKER_ADDFIELDS_TPL." (af_name, af_descr, af_subject, af_data) VALUES('".$db->sql_escape($_POST['af_name'][$k])."', '{$_POST['af_descr'][$k]}', '{$_POST['af_subject'][$k]}', '')";
				$result=$db->sql_query($sql);
			}
			else if(@$_POST['af_name'][$k]!='')
			{
				$sql='UPDATE '.TRACKER_ADDFIELDS_TPL." SET af_name='".$db->sql_escape($_POST['af_name'][$k])."', af_descr='{$_POST['af_descr'][$k]}', af_subject='{$_POST['af_subject'][$k]}' WHERE id='".my_int_val($k)."'";
				$result=$db->sql_query($sql);
			}
		}
	}
	if($d_addfields)
	{
		$sql='DELETE FROM '.TRACKER_ADDFIELDS_TPL." WHERE id IN('".implode("', '", $d_addfields)."')";
		$result=$db->sql_query($sql);

		$sql='DELETE FROM '.TRACKER_ADDFIELDS_SETS." WHERE af_id IN('".implode("', '", $d_addfields)."')";
		$result=$db->sql_query($sql);
	}
}
$addfields_title='ACP_TRACKER_ADDFIELDS_TPL';
if(request_var('add_sets', 0) && ($tfield=request_var('tfield', 0)))
{
	$d_af=array();
	foreach(@$_POST['af_name'] as $k=>$v)
	{
		if(@$_POST['af_delete'][$k])
		{
			$d_af[]=my_int_val($k);
		}
		else if ($k==0 && @$_POST['af_name'][$k]!='')
		{
			$sql='INSERT INTO '.TRACKER_ADDFIELDS_SETS."
				(af_id, af_name, af_required, af_order, af_count)
					VALUES('".my_int_val($tfield)."', '".$db->sql_escape($_POST['af_name'][$k])."', '".my_int_val(@$_POST['af_required'][$k])."', '".my_int_val($_POST['af_order'][$k])."', '".my_int_val($_POST['af_count'][$k])."')";
			$result=$db->sql_query($sql);
		}
		else if(@$_POST['af_name'][$k]!='')
		{
			$sql='UPDATE '.TRACKER_ADDFIELDS_SETS." SET af_name='".$db->sql_escape($_POST['af_name'][$k])."', af_required='".my_int_val(@$_POST['af_required'][$k])."', af_order='".my_int_val($_POST['af_order'][$k])."', af_count='".my_int_val($_POST['af_count'][$k])."' WHERE id='".my_int_val($k)."'";
			$result=$db->sql_query($sql);
		}
	}
	if($d_af)
	{
		$sql='DELETE FROM '.TRACKER_ADDFIELDS_SETS." WHERE id IN('".implode("', '", $d_af)."')";
		$result=$db->sql_query($sql);
	}
	$this->u_action=append_sid("{$phpbb_admin_path}index.$phpEx", 'i=board&amp;mode=addfields&amp;view_sets=1&amp;tfield='.$tfield);
}
if(request_var('copy_sets', 0))
{
	$tfield=request_var('tfield', 0);
	if($tfield)
	{
		$sql='SELECT * FROM '.TRACKER_ADDFIELDS_TPL." WHERE id='{$tfield}' LIMIT 1";
		$result=$db->sql_query($sql);
		$sets_name=$db->sql_fetchrow($result);
		$db->sql_freeresult($result);
		if($sets_name)
		{
			$sql='INSERT INTO '.TRACKER_ADDFIELDS_TPL." (af_name, af_descr, af_subject, af_data) VALUES('".$db->sql_escape($user->lang['SETS_COPY'].': '.$sets_name['af_name'])."', '".$db->sql_escape($sets_name['af_descr'])."', '".$db->sql_escape($sets_name['af_subject'])."', '".$db->sql_escape($sets_name['af_data'])."')";
			$result=$db->sql_query($sql);
			$sets_next=$db->sql_nextid();

			$sql='SELECT af_name, af_order, af_required, af_count FROM '.TRACKER_ADDFIELDS_SETS." WHERE af_id='{$tfield}'";
			$result=$db->sql_query($sql);
			while($row=$db->sql_fetchrow($result))
			{
				$sql='INSERT INTO '.TRACKER_ADDFIELDS_SETS."
				(af_id, af_name, af_required, af_order, af_count)
					VALUES('".my_int_val($sets_next)."', '".$db->sql_escape($row['af_name'])."', '".my_int_val($row['af_required'])."', '".my_int_val($row['af_order'])."', '".my_int_val($row['af_count'])."')";
				$result2=$db->sql_query($sql);
			}
			$db->sql_freeresult($result);
		}
	}
}
if(request_var('view_sets', 0))
{
	$user->add_lang('mods/ppkbb3cker_addfields');
	asort($user->lang['TRACKER_FORUM_ADDFIELDS']);
	$tfield=request_var('tfield', 0);
	$addfields_title='ACP_TRACKER_ADDFIELDS';
	if($tfield)
	{
		$sql='SELECT af_name FROM '.TRACKER_ADDFIELDS_TPL." WHERE id='{$tfield}'";
		$result=$db->sql_query($sql);
		$sets_name=$db->sql_fetchrow($result);
		$db->sql_freeresult($result);

		$sql='SELECT id, af_name, af_order, af_required, af_count FROM '.TRACKER_ADDFIELDS_SETS." WHERE af_id='{$tfield}' ORDER BY af_order";
		$result=$db->sql_query($sql);
		$af_exists=array();
		while($row=$db->sql_fetchrow($result))
		{
			$af_exists[$row['af_name']]=1;
			$template->assign_block_vars('sets', array(
				'ID'	=> $row['id'],
				'NAME'	=> $row['af_name'],
				'VALUE'	=> htmlspecialchars(@$user->lang['TRACKER_FORUM_ADDFIELDS'][$row['af_name']]),
				'ORDER'	=> $row['af_order'],
				'REQUIRED' => $row['af_required'],
				'COUNT'	=> $row['af_count'],
				)
			);
		}
		$db->sql_freeresult($result);
		$template->assign_block_vars('sets', array(
				'ID'	=> 0,
				'NAME'	=> '',
				'VALUE'	=> '',
				'ORDER'	=> 450,
				'REQUIRED' => 0,
				'COUNT'	=> 0,
				)
			);
		$af_selects='<select onChange="this.form[\'af_name[0]\'].value=this.form.af_selects.value;" name="af_selects"><option value=""></option>';
		foreach($user->lang['TRACKER_FORUM_ADDFIELDS'] as $k=>$v)
		{
			$af_disabled=@$af_exists[$k] ? ' disabled="disabled"' : '';
			$af_selects.='<option value="'.$k.'"'.$af_disabled.'>'.($af_disabled ? '-- ' : '').$v.(@$user->lang['TRACKER_FORUM_ADDFIELDS_INALL'][$k] ? ' ###' : '').'</option>';
		}
		$af_selects.='</select>';
		$template->assign_vars(array(
			'AF_SELECTS' => $af_selects,
			'AF_NAME' => htmlspecialchars($sets_name['af_name']),
			'S_HIDDEN_FIELDS'=>'<input type="hidden" name="add_sets" value="1" >
						<input type="hidden" name="tfield" value="'.$tfield.'" >',
			)
		);
	}
	$template->assign_vars(array(
		'S_SET_SETS'	=> true,
		)
	);
}
else
{
	$sql='SELECT * FROM '.TRACKER_ADDFIELDS_TPL.' ORDER BY id DESC';
	$result=$db->sql_query($sql);
	while($row=$db->sql_fetchrow($result))
	{
		$template->assign_block_vars('addfields', array(
			'COUNT'	=> $row['id'],
			'NAME'	=> htmlspecialchars($row['af_name']),
			'DESCR'	=> htmlspecialchars($row['af_descr']),
			'SUBJECT' => htmlspecialchars($row['af_subject']),
			'A_VIEW_SETS'	=> append_sid("{$phpbb_admin_path}index.$phpEx", 'i=board&amp;mode=addfields&amp;view_sets=1&amp;tfield='.$row['id']),
			'A_COPY_SETS'	=> append_sid("{$phpbb_admin_path}index.$phpEx", 'i=board&amp;mode=addfields&amp;copy_sets=1&amp;tfield='.$row['id']),
			)
		);
	}
	$db->sql_freeresult($result);
	$template->assign_block_vars('addfields', array(
			'COUNT'	=> 0,
			'NAME'	=> '',
			'DESCR'	=> '',
			'SUBJECT'	=> '',
			)
		);

	$template->assign_vars(array(
		'S_VIEW_ADDFIELDS'	=> true,
		'S_HIDDEN_FIELDS'=>'<input type="hidden" name="add_addfields" value="1" >',
		)
	);
}
$display_vars = array(
	'title'	=> $addfields_title,
	'vars'	=> array(
		'legend1'				=> 'ACP_TRACKER_ADDFIELDS_TPL_SETTINGS',
		)
	);
$template->assign_vars(array(
	'S_ADDFIELDS_INC'	=> true,
	)
);
?>
